package com.gitee.ywj1352.server.factory.impl;

import com.gitee.ywj1352.common.util.ServerStructure;
import com.gitee.ywj1352.server.ServerFactory;

import java.util.HashMap;
import java.util.Map;


/**
 * just get ServerStructure
 *
 * @author wenjieyang
 */
public class DefaultServerFactory implements ServerFactory {
    private final DefaultServerParser serverParser;
    private final Map<String, ServerStructure> cache = new HashMap<>();

    public DefaultServerFactory() {
        this.serverParser = new DefaultServerParser();
    }

    @Override
    public <T> void initProcessServer(Class<T> serverClazz, Object serverInstance) {
        ServerStructure parse = serverParser.parse(serverClazz, serverInstance);
        cache.put(parse.getServiceName(), parse);
    }


    @Override
    public ServerStructure getServer(String serverKey) {
        if (cache.containsKey(serverKey)) {
            return cache.get(serverKey);
        }
        throw new RuntimeException("not find exception");
    }

}
